package ldk.app.kotlinweibo.activities

import android.app.ProgressDialog
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import com.sina.weibo.sdk.WbSdk
import com.sina.weibo.sdk.auth.AuthInfo
import com.sina.weibo.sdk.auth.Oauth2AccessToken
import com.sina.weibo.sdk.auth.WbAuthListener
import com.sina.weibo.sdk.auth.WbConnectErrorMessage
import com.sina.weibo.sdk.auth.sso.SsoHandler
import com.sina.weibo.sdk.utils.LogUtil
import dagger.android.AndroidInjection
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_login.*
import ldk.app.kotlinweibo.R
import ldk.app.kotlinweibo.apis.APIErrorCommon
import ldk.app.kotlinweibo.configs.Session
import ldk.util.logger.LLog
import retrofit2.Retrofit
import javax.inject.Inject

/**
 * Login page
 * Created by yunjiao on 2017/6/15.
 */
class LoginActivity : AppCompatActivity(), WbAuthListener {
    private val TAG = "LoginActivity"

    @Inject
    internal lateinit var mSession: Session
    @Inject
    internal lateinit var mRetrofit: Retrofit

    private var mSsoHandler : SsoHandler? = null

    private val mLoading: ProgressDialog by lazy {
        ProgressDialog.show(this, getString(R.string.loading), getString(R.string.user_info_loading))
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        LogUtil.sIsLogEnable = true
        WbSdk.install(this, AuthInfo(this, "802235524", "https://api.weibo.com/oauth2/default.html", "email,direct_messages_read,direct_messages_write"))
        mSsoHandler = SsoHandler(this)

        btn_login.setOnClickListener{
            LLog.v(TAG, "clicked login button")
            mSsoHandler!!.authorize(this)
        }
    }


    override fun onSuccess(p0: Oauth2AccessToken?) {
        LLog.v(TAG, "onSuccess, $p0")
        btn_login.visibility = View.GONE
        mSession.loginInternal(p0!!)
        mLoading.show()
        mSession.currentUser(mRetrofit)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        {
                            LLog.v(TAG, "getUserInfo $it")
                            mLoading.dismiss()
                            APIErrorCommon.toastMessage(this, it.screen_name + "登陆成功")
                            finish()
                            startActivity(Intent(this, MainActivity::class.java))
                        },
                        {
                            APIErrorCommon.toastMessage(this, it.message)
                            mLoading.dismiss()
                        })
    }

    override fun onFailure(p0: WbConnectErrorMessage?) {
        LLog.v(TAG, "onFailure, $p0")
    }

    override fun cancel() {
        LLog.v(TAG, "cancel")
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LLog.v(TAG, "onActivityResult %d", resultCode)
        mSsoHandler?.authorizeCallBack(requestCode, resultCode, data)
    }
}